package org.tinygroup.remoteproperties.listener;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.tinygroup.commons.io.StreamUtil;
import org.tinygroup.remoteconfig.RemoteConfigReadClient;
import org.tinygroup.remoteconfig.utils.RemoteConfigHandler;
import org.tinygroup.remoteconfig.utils.TinyConfigParamUtil;
import org.tinygroup.remoteconfig.zk.manager.impl.ZKConfigClientImpl;

public class RemoteProSpringApplicationRunListener implements SpringApplicationRunListener {

    public RemoteProSpringApplicationRunListener(SpringApplication application, String[] args) {
    }

    @Override
    public void starting() {
        TinyConfigParamUtil.setReadClient(new ZKConfigClientImpl());
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
    	InputStream inputStream = this.getClass().getResourceAsStream(
                "/application.xml");
		try {
			String applicationConfig = StreamUtil.readText(inputStream, "UTF-8", true);
			// 远程配置
			RemoteConfigReadClient client = TinyConfigParamUtil.getReadClient();
			RemoteConfigHandler remoteConfig = new RemoteConfigHandler(
					applicationConfig, client);
			remoteConfig.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {

    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {

    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) {

    }
}
